package com.bto.solar.work.controller;

import com.bto.solar.work.common.R;
import com.bto.solar.work.entity.Record;
import com.bto.solar.work.service.BtoRecordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 备案证管理
 * 备案信息（后台提前录入，业务直接调用） 前端控制器
 * </p>
 *
 * @author adrian
 * @since 2023-08-09
 */
@RestController
@RequestMapping("record")
public class RecordController {
    @Autowired
    private BtoRecordService btoRecordService;

    /**
    * 创建备案证
    */
    @PostMapping("new")
    public R createNewRecord(@Valid @RequestBody Record record){
        btoRecordService.createRecord(record);
        return R.ok();
    }
    /**
     *  更新备案证
     */
    @PostMapping("update")
    public R updateRecord(@Valid @RequestBody Record record){
        btoRecordService.updateRecord(record);
        return R.ok();
    }

    /**
     *  删除备案证
     */
    @DeleteMapping("delete")
    public R deleteRecord(@ApiParam(required = true) String recordCertificateId){
        btoRecordService.deleteRecord(recordCertificateId);
        return R.ok();
    }


    /**
     *  分页查询备案证
     */
    @GetMapping("one")
    public R getRecordOne(@RequestParam String recordCertificateId){
        return R.ok(btoRecordService.getRecordById(recordCertificateId));
    }


    /**
     *  分页查询备案证
     */
    @PostMapping("get")
    public R getRecord(@RequestParam(required = false, defaultValue = "1")  Integer pageNum ,
                       @RequestParam(required = false, defaultValue = "20")  Integer pageSize ,
                       @RequestBody(required = false) Record record){
        PageHelper.startPage(pageNum , pageSize);
        return R.ok(PageInfo.of(btoRecordService.getRecords(record, false)));
    }

    /**
     *  查询备案证
     */
    @PostMapping("list")
    public R getRecordList(@RequestBody(required = false) Record record,
                           @RequestParam(required = false, defaultValue = "true") boolean isAuth){
        return R.ok(btoRecordService.getRecords(record, isAuth));
    }

    /**
     *  查询备案证数量
     */
    @GetMapping("count")
    public R getRecordList(){
        return R.ok(btoRecordService.getRecordCount());
    }
}
